package cn.newtouch.org.controller;

import cn.newtouch.basic.annotation.PreAuthorize;
import cn.newtouch.basic.util.JsonResult;
import cn.newtouch.basic.util.PageList;
import cn.newtouch.org.domain.Department;
import cn.newtouch.org.query.DepartmentQuery;
import cn.newtouch.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    //回显上级部门树
    @GetMapping("/deptTree")
    @ApiOperation(value = "获取部门树-无限极接口")
    @PreAuthorize(name = "部门树",sn = "department:tree")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有部门" )
    @PreAuthorize(name = "所有部门",sn = "department:getAll")
    public List<Department> findAll(){
        return departmentService.loadAll();
    }
    /**
     * 查询一个
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(name = "通过ID查询部门",sn = "department:get")
    public Department findOne(@PathVariable Long id){
       return departmentService.loadById(id);
    }

    /**
     * 删除
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    @PreAuthorize(name = "删除部门",sn = "department:delete")
    public JsonResult deleteById(@PathVariable("id") Long id){
        try {
            departmentService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
     * 添加修改
     * @return
     */
    @PutMapping
    @ApiOperation(value = "没有参数ID是添加或者有ID是修改" )
    @PreAuthorize(name = "保存部门",sn = "department:save")
    public JsonResult updateOrAdd(@RequestBody Department department){
        try {
            if(department.getId()==null){
                //添加操作
                departmentService.save(department);
            }else{
                //修改操作
                departmentService.update(department);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }

    /**
     * 分页
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询" )
    @PreAuthorize(name = "部门分页查询",sn = "department:list")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        try {
            return departmentService.queryPage(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }

    }

    /**
     * 批量删除
     * @return
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除" )
    @PreAuthorize(name = "部门批量删除",sn = "department:patchDel")
    public JsonResult patchDel(@RequestBody List<Long> ids){
        try {
            departmentService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败！");
        }
    }


}
